Conditions | 10 |
Total Lines | 82 |
Code Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like OSPaths.ts ➔ OSPathsAdaptionBuilder_ often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | // # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey |
||
15 | |||
16 | // eslint-disable-next-line @typescript-eslint/no-namespace |
||
17 | namespace Adapt { |
||
18 | export const isWinOS = (adapter_: Platform.Adapter) => /^win/i.test(adapter_.process.platform); |
||
19 | |||
20 | export const normalizePath = (adapter_: Platform.Adapter) => { |
||
21 | return (path_: string | undefined): string | undefined => { |
||
22 | return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0; |
||
23 | }; |
||
24 | }; |
||
25 | |||
26 | export const home = (adapter_: Platform.Adapter) => { |
||
27 | const { env, os, path } = adapter_; |
||
28 | |||
29 | const isWinOS = Adapt.isWinOS(adapter_); |
||
30 | const normalizePath = Adapt.normalizePath(adapter_); |
||
31 | |||
32 | const posix = () => |
||
33 | normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME')); |
||
34 | |||
35 | const windows = () => { |
||
36 | const priorityList = [ |
||
37 | typeof os.homedir === 'function' ? os.homedir() : void 0, |
||
38 | env.get('USERPROFILE'), |
||
39 | env.get('HOME'), |
||
40 | env.get('HOMEDRIVE') || env.get('HOMEPATH') |
||
41 | ? path.join(env.get('HOMEDRIVE') || '', env.get('HOMEPATH') || '') |
||
42 | : void 0, |
||
43 | ]; |
||
44 | return normalizePath(priorityList.find((v) => !isEmpty(v))); |
||
45 | }; |
||
46 | |||
47 | return isWinOS ? windows : posix; |
||
48 | }; |
||
49 | |||
50 | export const temp = (adapter_: Platform.Adapter) => { |
||
51 | const { env, os, path } = adapter_; |
||
52 | |||
53 | const isWinOS = Adapt.isWinOS(adapter_); |
||
54 | const normalizePath = Adapt.normalizePath(adapter_); |
||
55 | |||
56 | function joinPathToBase(base: string | undefined, segments: readonly string[]) { |
||
57 | return base ? path.join(base, ...segments) : void 0; |
||
58 | } |
||
59 | |||
60 | const posix = () => { |
||
61 | const fallback = '/tmp'; |
||
62 | const priorityList = [ |
||
63 | typeof os.tmpdir === 'function' ? os.tmpdir() : void 0, |
||
64 | env.get('TMPDIR'), |
||
65 | env.get('TEMP'), |
||
66 | env.get('TMP'), |
||
67 | ]; |
||
68 | return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback; |
||
69 | }; |
||
70 | |||
71 | const windows = () => { |
||
72 | const fallback = 'C:\\Temp'; |
||
73 | const priorityListLazy = [ |
||
74 | os.tmpdir, |
||
75 | () => env.get('TEMP'), |
||
76 | () => env.get('TMP'), |
||
77 | () => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']), |
||
78 | () => joinPathToBase(Adapt.home(adapter_)(), ['AppData', 'Local', 'Temp']), |
||
79 | () => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']), |
||
80 | () => joinPathToBase(env.get('SystemRoot'), ['Temp']), |
||
81 | () => joinPathToBase(env.get('windir'), ['Temp']), |
||
82 | () => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']), |
||
83 | ]; |
||
84 | const v = priorityListLazy.find((v) => v && !isEmpty(v())); |
||
85 | return (v && normalizePath(v())) || fallback; |
||
86 | }; |
||
87 | |||
88 | return isWinOS ? windows : posix; |
||
89 | }; |
||
90 | } |
||
91 | |||
92 | export function OSPathsAdaptionBuilder_(adapter_: Platform.Adapter): OSPaths { |
||
93 | // eslint-disable-next-line functional/no-class |
||
94 | class OSPaths_ { |
||
95 | constructor() { |
||
96 | const OSPaths = function () { |
||
97 | return new OSPaths_() as OSPaths; |
||
109 |